package com.servlets;

import com.alibaba.fastjson.JSON;
import com.pojo.PageBean;
import com.pojo.Users;
import com.service.UsersService;
import com.service.impl.UsersServiceImpl;
import com.utils.JDBCUtil;
import com.utils.MD5Utils;
import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/Users.do")
public class UsersServlet extends BaseServlet {
    private UsersService service = new UsersServiceImpl();

    //查询所有用户
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String current = request.getParameter("current");
        String limit = request.getParameter("limit");
        Integer curr=1,lim = 10;
        if (current!=null)
            curr=Integer.parseInt(current);
        if (limit!=null)
            lim = Integer.parseInt(limit);

        PageBean<Users> list = service.findAllUsers(lim,curr);
        response.getWriter().write(JDBCUtil.toLayuiJson(list.getTotal(),list.getData()));
    }

    //登录方法
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String vercode = request.getParameter("vercode");
        boolean chk = Boolean.getBoolean(request.getParameter("chk"));
        String msg = "";
        response.setContentType("application/json;charset=utf-8");
        if (CaptchaUtil.ver(vercode, request)){
            Map<String, String[]> map = request.getParameterMap();
            Users users = JDBCUtil.mapToBean(map, Users.class);
            users.setPassword(MD5Utils.encryption(users.getPassword()));
            Users users1 = service.login(users);
            if (users1!=null){

                if(users1.getType()==1){
                    request.getSession().setAttribute("reception",users1);
                    msg = "前台用户";
                }else{
                    request.getSession().setAttribute("admin",users1);
                    msg = "后台用户";
                }

            }else {
                msg = "密码错误，登录失败";
            }



        }else {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            msg="验证码错误";
        }
        response.getWriter().write(JSON.toJSONString(msg));
    }

    //退出登录方法
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String s = request.getParameter("s");
        request.getSession().removeAttribute(s);
        if (s.equals("admin")){
            response.sendRedirect("admin/login.jsp");
        }
        response.sendRedirect("reception/login.jsp");
    }

    //删除用户方法
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        response.setContentType("application/json;charset=utf-8");
        String msg = "";
        if(service.deleteUser(id)){
            msg="删除成功";
        }else {
            msg="删除失败";
        }
        response.getWriter().write(JSON.toJSONString(msg));
    }

    //添加用户方法
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        Map<String, String[]> map = request.getParameterMap();
        Users users = JDBCUtil.mapToBean(map, Users.class);
        users.setPassword(MD5Utils.encryption(users.getPassword()));
        users.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        String msg="";
        if(service.addUser(users)){
            msg="成功";
        }else {
            msg="失败";
        }
        response.getWriter().write(JSON.toJSONString(msg));
    }

    //根据ID查询用户
    public void findByID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        String  id = request.getParameter("id");
        Users users = service.findUsersByID(id);
        response.getWriter().write(JSON.toJSONString(users));
    }

    //修改用户方法
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, String[]> map = request.getParameterMap();
        Users users = JDBCUtil.mapToBean(map, Users.class);
        users.setPassword(MD5Utils.encryption(users.getPassword()));
        response.setContentType("application/json;charset=utf-8");
        String msg = "";
        if (service.updateUser(users)){
            msg="成功";
        }else {
            msg="失败";
        }
        response.getWriter().write(JSON.toJSONString(msg));
    }

    //查询是否有重复用户名
    public void findUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String username = request.getParameter("username");
        String msg = "";
        if (service.findUsername(username)){
            msg = "用户名重复";
        }else {
            msg="用户名可用";
        }
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(msg));
    }


}
